/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.client.mvc.controller;

import java.util.List;

import org.lifebudget.app.client.events.AppEvents;
import org.lifebudget.app.client.mvc.model.Transaction;
import org.lifebudget.app.client.mvc.model.TransactionData;
import org.lifebudget.app.client.mvc.view.TransactionJournalView;
import org.lifebudget.app.client.service.TransactionService;
import org.lifebudget.app.client.service.TransactionServiceAsync;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Controller for the {@link TransactionJournalView}, handling all Transaction
 * related events and views.
 */
public class TransactionJournalController extends Controller {
	
	/** The transactions view. */
	private TransactionJournalView transactionsView;
	
	/** The transaction service. */
	private TransactionServiceAsync transactionService;

	/**
	 * Instantiates a new transaction journal controller, and registers all
	 * of the events handled by the controller.
	 */
	public TransactionJournalController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.TransactionChangesCommitted);
		registerEventTypes(AppEvents.TransactionRemoved);
		registerEventTypes(AppEvents.RefreshCategory);
		registerEventTypes(AppEvents.RefreshPaycheck);
	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.Controller#initialize()
	 */
	public void initialize() {
		transactionsView = new TransactionJournalView(this);
		transactionService = GWT.create(TransactionService.class);
	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.Controller#handleEvent(com.extjs.gxt.ui.client.mvc.AppEvent)
	 */
	@Override
	public void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			onInit(event);

		}
		if (event.getType() == AppEvents.TransactionChangesCommitted) {
			onTransactionChangesCommitted(event);
		}
		if (event.getType() == AppEvents.TransactionRemoved) {
			onTransactionRemoved(event);
		}
		if (event.getType() == AppEvents.RefreshCategory) {
			onRefreshCategories(event);
		}
		if (event.getType() == AppEvents.RefreshPaycheck) {
			onRefreshPaychecks(event);
		}
	}

	/**
	 * Handles the <code>RefreshPaycheck</code> event, forwarding to the 
	 * {@link TransactionJournalView}.
	 *
	 * @param event the <code>RefreshPaycheck</code> event
	 */
	private void onRefreshPaychecks(final AppEvent event) {
		GWT.log("RefreshPaycheck recieved on Transactions controller");
		forwardToView(transactionsView, event);
	}

	/**
	 * Handles the <code>RefreshCategories</code> event, forwarding to the 
	 * {@link TransactionJournalView}.
	 *
	 * @param event the <code>RefreshCategories</code> event
	 */
	private void onRefreshCategories(final AppEvent event) {
		GWT.log("RefreshCategory recieved on Transactions controller");
		forwardToView(transactionsView, event);
	}

	/**
	 * Handles the <code>TransactionRemoved</code> event, invoking the service to 
	 * remove the transaction and then dispatching a <code>RefreshTransactions</code> event.
	 *
	 * @param event the <code>TransactionRemoved</code> event
	 */
	private void onTransactionRemoved(AppEvent event) {
		Transaction t = (Transaction) event.getData();
		transactionService.removeTransaction(t, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.get().dispatch(AppEvents.Error);

			}

			@Override
			public void onSuccess(Void result) {
				Dispatcher.get().dispatch(AppEvents.RefreshTransaction);

			}

		});

	}

	/**
	 * Handles the <code>TransactionChangesCommited</code> event, saving the
	 * transactions passed in with the event and then forwarding the updated Transaction
	 * list to the {@link TransactionJournalView}.
	 *
	 * @param event the <code>TransactionChangesCommited</code> event
	 */
	private void onTransactionChangesCommitted(final AppEvent event) {
		List<Transaction> transactions = (List<Transaction>) event.getData();
		transactionService.saveTransactions(transactions, new AsyncCallback<List<Transaction>>() {

			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.get().dispatch(AppEvents.Error);
			}

			@Override
			public void onSuccess(List<Transaction> result) {				
				event.setData(result);
				forwardToView(transactionsView, event);

			}

		});

	}

	/**
	 * Handles the <code>Init</code> event, getting all transactions and forwarding to
	 * the {@link TransactionJournalView}.
	 *
	 * @param event the <code>Init</code> event
	 * 
	 */
	private void onInit(final AppEvent event) {
		// GWT.log("TransactionJournalController received AppEvent Init, forwarding to view");
		transactionService.getTransactionData(new AsyncCallback<TransactionData>() {

			@Override
			public void onFailure(Throwable caught) {
				Dispatcher.get().dispatch(AppEvents.Error);
			}

			@Override
			public void onSuccess(TransactionData result) {
				event.setData(result);
				forwardToView(transactionsView, event);
			}
		});
	}

}
